package com.xywl.escort.adapter.provider;

import android.text.TextUtils;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.annotation.ItemProviderTag;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.xywl.escort.R;
import com.xywl.escort.adapter.HomeRvAdapter;
import com.xywl.escort.adapter.NetworkImageHolderView;
import com.xywl.escort.bean.BannerResp;
import com.xywl.escort.entity.NormalMultipleEntity;

import java.util.ArrayList;
import java.util.List;

@ItemProviderTag(
        viewType = HomeRvAdapter.TYPE_BANNER,
        layout = R.layout.item_banner_view
)
public class BannerItemProvider extends BaseItemProvider<NormalMultipleEntity, BaseViewHolder> {

    @Override
    public void convert(BaseViewHolder helper, final NormalMultipleEntity data, int position) {
        ConvenientBanner convenientBanner = helper.getView(R.id.convenientBanner);

        if (null != data.mBannerData) {
            List<BannerResp.DataBean> list = data.mBannerData;
            List<String> bannerList = new ArrayList<>();
            for (BannerResp.DataBean bean : list) {
                bannerList.add(bean.imgUrl);
            }
            convenientBanner.setPages(new CBViewHolderCreator() {
                @Override
                public NetworkImageHolderView createHolder() {
                    return new NetworkImageHolderView();
                }
            }, bannerList).setPageIndicator(new int[]{
                    R.mipmap.home_banner2,
                    R.mipmap.home_banner1})
                    .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL)
                    .setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(int position) {
                            if (null != data.mBannerData) {
                                List<BannerResp.DataBean> bannerData = data.mBannerData;
                                BannerResp.DataBean listBean = bannerData.get(position);
                                if (null != listBean) {
                                    switch (listBean.linkType) {
                                        case 1:
                                            ARouter.getInstance().build("/xy/web/links")
                                                    .withString("url", listBean.linkContent)
                                                    .withString("title", mContext.getString(R.string.app_name)).navigation();
                                            break;
                                        case 2:
                                            ARouter.getInstance().build("/xy/project/detail")
                                                    .withString("projectId", listBean.linkContent)
                                                    .withString("title", "项目详情")
                                                    .navigation();
                                            break;
                                        case 3:
                                            String content = listBean.linkContent;
                                            if (!TextUtils.isEmpty(content)) {
                                                try {
                                                    ARouter.getInstance().build("/xy/product/detail").withString("spuId", content).navigation();
                                                } catch (NumberFormatException e) {
                                                }
                                            }
                                            break;
                                        default:
                                            break;
                                    }
                                }
                            }
                        }
                    })
                    .setCanLoop(bannerList.size() > 1);
            if (bannerList.size() > 1) {
                convenientBanner.startTurning(5000);
            }
        }
    }

    @Override
    public void onClick(BaseViewHolder helper, NormalMultipleEntity data, int position) {
    }

    @Override
    public boolean onLongClick(BaseViewHolder helper, NormalMultipleEntity data, int position) {
        return true;
    }
}
